#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include "Log.hpp"
const std::string dict_file = "./recource/dict_data.txt";

namespace zhong
{
    class Word
    {
        // 单词、音节、中文解释、类型
    };

    class Translate
    {
    private:
        void LoadDict()
        {
            std::ifstream in(_dict_path);
            std::string line;
            while (std::getline(in, line))
            {
                _lines.push_back(line);
            }
            in.close();
            lg.LogMessage(Info, "success to load dict, from: %s\n", _dict_path.c_str());
        }
        void Parse()
        {
            for (auto &line : _lines)
            {
                auto pos = line.find(" ");
                if (pos == std::string::npos)
                    continue;
                else
                {
                    // 截取单词
                    std::string word = line.substr(0, pos);
                    std::string chinese = line.substr(pos + 1);
                    _dict.insert(std::make_pair(word, chinese));
                }
            }
            lg.LogMessage(Info, "success to prase\n");
        }

    public:
        Translate(std::string dict_path = dict_file)
            : _dict_path(dict_path)
        {
            LoadDict();
            Parse();
        }
        ~Translate() {}
        std::string Excute(const std::string &word)
        {
            auto it = _dict.find(word);
            if (it == _dict.end())
            {
                return "unknown";
            }
            else
            {
                return _dict[word];
            }
        }
        void PrintLines()
        {
            for (auto &line : _lines)
            {
                std::cout << line << std::endl;
            }
        }
        void PrintDict()
        {
            for (auto &elem : _dict)
            {
                std::cout << elem.first << " : " << elem.second << std::endl;
            }
        }

    private:
        std::unordered_map<std::string, std::string> _dict;
        std::string _dict_path;

        std::vector<std::string> _lines; // dick
    };
}